package com.mic.yzzn.plc.common.control.datastructure;

import java.io.Serializable;
import java.util.Date;

import com.mic.yzzn.plc.common.instructions.basic.PLCRequestInstruction;

public class PLCData implements Serializable
{
	public static final String PLCDATA_ACTION_TYPE_READ = "PLCDATA_ACTION_TYPE_READ";
	public static final String PLCDATA_ACTION_TYPE_WRITE = "PLCDATA_ACTION_TYPE_WRITE";
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -624953175671261450L;
	/** 数据的时间戳 */
	private Date timestamp = new Date();

	/** 数据对应的类型 */
	private String pointType;

	/** 数据对应的硬件编号 e.g. 2000 or 20000*/
	private String dataNumber;
	
	/** 点位|操作结果状态: OK{200:操作成功} FAIL{400:错误 404:该点位不存在 405:超时 406:连接失败 code:PLC返回} */
	private int state;
	
	private String instructionCode;
	
	/** 数据的内容 e.g. (Read OK{R: 1 or 0, D: FFFF}; fail: {! or null}) (write ok:{$}, fail:{!})*/
	private String data;
	/** 指令的内容 e.g. */
	private String instruction;
	
	
	public String getActionType()
	{
		if (PLCRequestInstruction.readInsturctionsCode().contains(instructionCode))
		{
			return PLCDATA_ACTION_TYPE_READ;
		}
		else
		{
			return PLCDATA_ACTION_TYPE_WRITE;
		}
	}
	
	@Override
	public String toString()
	{
		return (dataNumber + " : " + data + " state: " + state);
	}
	
	// Getter
	public Date getTimestamp()
	{
		return timestamp;
	}
	
	public String getData()
	{
		return data;
	}
	
	public String getDataNumber()
	{
		return dataNumber;
	}
	
	public String getInstruction()
	{
		return instruction;
	}
	
	public String getInstructionCode()
	{
		return instructionCode;
	}
	
	public int getState()
	{
		return state;
	}

	public String getPointType() {
		return pointType;
	}

	// Setter
	public void setDataNumber(String dataNumber)
	{
		this.dataNumber = dataNumber;
	}
	
	public void setData(String data)
	{
		this.data = data;
	}
	
	public void setTimestamp(Date timestamp)
	{
		this.timestamp = timestamp;
	}
	
	public void setInstruction(String instruction)
	{
		this.instruction = instruction;
	}
	
	public void setInstructionCode(String instructionCode)
	{
		this.instructionCode = instructionCode;
	}
	
	public void setState(int state)
	{
		this.state = state;
	}

	public void setPointType(String pointType) {
		this.pointType = pointType;
	}

	/**
	 * 检测数据是否正确
	 * @return
     */
	public boolean isCorrect()
	{
		return data.equals("!");
	}
}
